package com.example.health;

import java.sql.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	Context self;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		self = this;
		setContentView(R.layout.activity_main);
		WebServiceHandler wsh = new WebServiceHandler();
		wsh.setURL("http://192.168.1.19:8080/WebHeart/webresources/");
		JSONArray medicalStaffs =new JSONArray();
		final DatabaseHandler db = new DatabaseHandler(this);
		JSONObject obj = new JSONObject();
		try {
			medicalStaffs = wsh.callWebService("net.etfbl.healt.medicalstaff");
			for(int i = 0; i<medicalStaffs.length();i++){
				obj = medicalStaffs.getJSONObject(i);
				db.addMedicalStaff(new MedialStaff(obj.getInt("id"), obj.getString("username"), obj.getString("password"), 
									obj.getString("firstName"), obj.getString("lastName"), obj.getString("title"), obj.getString("type"), new Date(new java.util.Date().getTime())));
			}
		} catch (JSONException e) {
			System.out.println(e.getMessage());
		}
		
		Button loginBtn = (Button) findViewById(R.id.button1);
		loginBtn.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText password = (EditText) findViewById(R.id.editText2);
				EditText username = (EditText) findViewById(R.id.editText1);
				if(db.login(username.getText().toString(), password.getText().toString()).equals("success")){
					Intent nextSreen = new Intent(self, Selector.class);
					startActivity(nextSreen);
				}
				else{
					AlertDialog.Builder alertBuilder = new AlertDialog.Builder(self);
					alertBuilder.setTitle("Wrong parameters");
					alertBuilder.setMessage("Username or password is incorrect!");
					alertBuilder.setNegativeButton("OK",new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							// if this button is clicked, just close
							// the dialog box and do nothing
							dialog.cancel();
						}
					});
					AlertDialog alert = alertBuilder.create();
					alert.show();
				}
			}
		});
//		WebServiceHandler ws = new WebServiceHandler("http://localhost:8080/healt/webresources/net.etfbl.healt.medicalstaff");
//		ws.callWebService("");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
